//
//  DDCompanyHomeViewController.m
//  DingDongZP
//
//  Created by weily on 16/4/24.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDCompanyHomeViewController.h"
#import "DDBossEditInfoViewController.h"
#import "DDEditInfoCell.h"
#import "DDBossInfoService.h"
#import "DDBossInfo.h"
#import "DDInfoEditViewController.h"
#import "DDBossTagViewController.h"
#import "DDBossSetPositionViewController.h"
#import "DDIndustrySelectViewController.h"
#import "DDCompanySize.h"
#import "DDCompanyFinance.h"
#import "DDTextFieldEditViewController.h"
#import "DDTextViewEditViewController.h"
#import "DDDefaultsAvatarView.h"
#import "DDAvatarPickerView.h"
#import "CCImageGetter.h"
#import "DDCompanyImageViewController.h"
#import "DDCompanyHomeService.h"

/**
 *  公司主页
 */

@interface DDCompanyHomeViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
UIImagePickerControllerDelegate,
UINavigationControllerDelegate
>

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *sectionTitles;

@property (nonatomic, strong) DDBossInfoService *service;
@property (nonatomic, strong) DDBossInfo *userInfo;
@property (nonatomic, strong) NSArray *sizeArray;
@property (nonatomic, strong) NSArray *financeArray;

@property (nonatomic, strong) DDCompanyInfo *companyInfo;

@end

@implementation DDCompanyHomeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"公司主页";
    self.titleArray = @[@[@"头像", @"姓名", @"高管身份", @"高管介绍"],
                        @[@"公司LOGO", @"公司风采图片"],
                        @[@"产品LOGO", @"产品名称", @"产品优势", @"这个产品的亮点是"]];
    self.sectionTitles = @[@"公司高管", @"公司风采", @"产品介绍"];
    self.tableView.sectionHeaderHeight = 40;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//    self.tableView.backgroundColor = COLOR_BACKGROUND_LIGHTGREEN;
}

- (void) viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self loadInfoData];
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
    
//    self.tableView.backgroundColor = COLOR_BACKGROUND_LIGHTGREEN;

}

#pragma mark - load
- (void)loadInfoData {
    [self showLoading];
    
    [[DDCompanyHomeService sharedService] queryCompanyInfo:^(id responseObject, NSError *error) {
        [self hideLoading];
        if ([responseObject isKindOfClass:[DDCompanyInfo class]]) {
            self.companyInfo = responseObject;
            [self.tableView reloadData];
        }
        else {
            if (error) {
                [self showErrorWithText:error.domain];
            }
        }
    }];
}

#pragma mark - delegates
#pragma mark -- tableView
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 && indexPath.section == 0) {
        return 70.f;
    }
    
    return 42.f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section < self.titleArray.count) {
        NSArray *titles = self.titleArray[section];
        return titles.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDEditInfoCell *cell = nil;
    if (indexPath.row == 0)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"head"];
        if (!cell) {
            cell = (DDEditInfoCell *)[[[NSBundle mainBundle] loadNibNamed:@"DDEditInfoHeaderCell" owner:self options:nil] lastObject];
        }
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = (DDEditInfoCell *)[[[NSBundle mainBundle] loadNibNamed:@"DDEditInfoCell" owner:self options:nil] lastObject];
        }
    }
    if (indexPath.section < self.titleArray.count && indexPath.row < [self.titleArray[indexPath.section] count]) {
        cell.titleLabel.text = self.titleArray[indexPath.section][indexPath.row];
    }
    
    //  删除默认信息
    cell.infoLabel.text = nil;
    cell.infoLabel.textColor = COLOR_FONT_LIGHT_GREEN;

    switch (indexPath.section)
    {
        case 0:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    cell.headerImageView.image = [UIImage imageNamed:@"common_avatar"];
                    if (self.companyInfo) {
                        NSString *url = [DDAPIGenerate imageURLWithPath:self.companyInfo.ceo_logo];
                        [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:url]
                                                placeholderImage:nil];
                    }
                    break;
                }
                case 1:
                {
                    cell.infoLabel.text = self.companyInfo.ceo_name;
                    break;
                }
                case 2:
                {
                    cell.infoLabel.text = self.companyInfo.ceo_position_customized ? self.companyInfo.ceo_position_customized : self.companyInfo.ceo_position_name;
                    break;
                }
                case 3:
                {
                    cell.infoLabel.text = self.companyInfo.ceo_intro;
                    break;
                }
                default:
                    break;
            }
            break;  // end of section 0
        }
        //  section 1
        case 1:
        {
            switch (indexPath.row)
            {
                case 0:
                {
//                    cell.headerImageView.image = [UIImage imageNamed:@"common_avatar"];
                    if (self.companyInfo) {
                        NSString *url = [DDAPIGenerate imageURLWithPath:self.companyInfo.company_logo];
                        [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:url]
                                                placeholderImage:nil];
                    }
                    break;
                }
                case 1:
                {
                    
                    break;
                }
                default:
                    break;
            }
            break;
        }
        //  section == 2
        case 2:
        {
            switch (indexPath.row)
            {
                case 0:
                    if (self.companyInfo) {
                        NSString *url = [DDAPIGenerate imageURLWithPath:self.companyInfo.product_logo];
                        [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:url]
                                                placeholderImage:nil];
                    }
                    break;
                case 1:
                    cell.infoLabel.text = self.companyInfo.product_name;
                    break;
                case 2:
                    cell.infoLabel.text = self.companyInfo.product_strengths;
                    break;
                case 3:
                    cell.infoLabel.text = self.companyInfo.product_highlights;
                    break;
                default:
                    break;
            }
            break;
        }
        default:
            break;
    }
    
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
//    view.backgroundColor = COLOR_BACKGROUND_GRAY;
    tableView.backgroundColor = COLOR_BACKGROUND_GRAY;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 50)];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont fontWithName:@"Helvetica" size:15];
    
    label.text = self.sectionTitles[section];
    label.textColor = COLOR_FONT_DARK_GRAY;
    
    [view addSubview:label];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0: {
            switch (indexPath.row) {
                case 0: {
                    [self editAvatar];
                    break;
                }
                case 1: {
                    [self editName];
                    break;
                }
                case 2:
                    [self editIdentity];    ///身份
                    break;
                case 3: {
                    [self editMajorIntro];  ///高管介绍
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case 1: {
            switch (indexPath.row) {
                case 0:
                    [self editCompanyLogo];
                    break;
                case 1:
                    [self editCompanyImages];
                default:
                    break;
            }
            break;
        }
        case 2: {
            switch (indexPath.row) {
                case 0:
                    [self editProductLogo];
                    break;
                case 1:
                    [self editProductName];
                    break;
                case 2:
                    [self editProductAdvantage];
                    break;
                case 3:
                    [self editProductLight];
                    break;
                default:
                    break;
            }
            break;
        }
        default:
            break;
    }
}

#pragma mark - functions
- (void)editAvatar {
    WEAKSELF;
    DDAvatarPickerView *avatar = [DDAvatarPickerView instanciate];
    [avatar showWithAnimation:YES];
    avatar.didSelectAvatarType = ^(NSInteger type) {
        if (type == 2) {
            DDDefaultsAvatarView *defaults = [DDDefaultsAvatarView instanciate];
            [defaults showWithAnimation:YES];
            
            defaults.didGetImage = ^(UIImage *img) {
                [weakSelf uploadImage:img];
            };
        }
        else if (type == 0) {
            [[CCImageGetter sharedImageGetter] takePhoto];
            [CCImageGetter sharedImageGetter].callback = ^(UIImage *img) {
                [weakSelf uploadImage:img];
            };
        }
        else if (type == 1) {
            [[CCImageGetter sharedImageGetter] selectFromAlbum];
            [CCImageGetter sharedImageGetter].callback = ^(UIImage *img) {
                [weakSelf uploadImage:img];
            };
        }
    };
}

- (void)uploadImage:(UIImage *)img {
    
    [self showLoading];
    [[DDCompanyHomeService sharedService] uploadCeoAvatar:img callback:^(BOOL succ) {
        [self hideLoading];
        if (succ) {
            [self showText:@"上传头像成功"];
            [self loadInfoData];
        }
        else {
            [self showText:@"头像上传失败，请重试"];
        }
    }];
}

- (void)editName
{
    DDTextFieldEditViewController *vc = [[DDTextFieldEditViewController alloc] init];
    vc.title = @"姓名";
    vc.value = self.companyInfo.ceo_name;
    vc.block = ^(NSString *value) {
        
        //  上传姓名
        [self showLoading];
        DDBossCEOInfo *ceo = [DDBossCEOInfo withN:value p:nil pna:nil in:nil];
        [[DDCompanyHomeService sharedService] setCEOInfo:ceo callback:^(BOOL succ, NSString *err) {
            [self hideLoading];
            if (succ) {
                [self loadInfoData];
            }
            else {
                [self showErrorWithText:err];
            }
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)editIdentity
{
    DDBossSetPositionViewController *vc = [[DDBossSetPositionViewController alloc] init];
    vc.title = @"高管身份";
    vc.positionName = self.companyInfo.ceo_position_name;
    vc.positionCustomName = self.companyInfo.ceo_position_customized;
    vc.didSelectPosition = ^(NSString *pid, NSString *pname) {
        
        //  上传职位
        [self showLoading];
        DDBossCEOInfo *ceo = [DDBossCEOInfo withN:nil p:pid pna:pname in:nil];
        [[DDCompanyHomeService sharedService] setCEOInfo:ceo callback:^(BOOL succ, NSString *err) {
            [self hideLoading];
            if (succ) {
                [self loadInfoData];
            }
            else {
                [self showErrorWithText:err];
            }
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)editMajorIntro
{
    DDTextViewEditViewController *vc = [[DDTextViewEditViewController alloc] initWithMaxCharacterNumber:500];
    vc.title = @"高管介绍";
    vc.value = self.companyInfo.ceo_intro;
    vc.block = ^(NSString *str) {
        
        //  上传介绍
        [self showLoading];
        DDBossCEOInfo *ceo = [DDBossCEOInfo withN:nil p:nil pna:nil in:str];
        [[DDCompanyHomeService sharedService] setCEOInfo:ceo callback:^(BOOL succ, NSString *err) {
            [self hideLoading];
            if (succ) {
                [self loadInfoData];
            }
            else {
                [self showErrorWithText:err];
            }
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)editCompanyLogo
{
    WEAKSELF;
    DDAvatarPickerView *avatar = [DDAvatarPickerView instanciate];
    avatar.showDefaultsImgs = NO;
    [avatar showWithAnimation:YES];
    avatar.didSelectAvatarType = ^(NSInteger type) {
        if (type == 2) {
            DDDefaultsAvatarView *defaults = [DDDefaultsAvatarView instanciate];
            [defaults showWithAnimation:YES];
            
            defaults.didGetImage = ^(UIImage *img) {
                [weakSelf uploadCompanyLogo:img];
            };
        }
        else if (type == 0) {
            [[CCImageGetter sharedImageGetter] takePhoto];
            [CCImageGetter sharedImageGetter].callback = ^(UIImage *img) {
                [weakSelf uploadCompanyLogo:img];
            };
        }
        else if (type == 1) {
            [[CCImageGetter sharedImageGetter] selectFromAlbum];
            [CCImageGetter sharedImageGetter].callback = ^(UIImage *img) {
                [weakSelf uploadCompanyLogo:img];
            };
        }
    };
}

- (void)uploadCompanyLogo:(UIImage *)img
{
    [self showLoading];
    [[DDCompanyHomeService sharedService] uploadCompanyLogo:img callback:^(BOOL succ) {
        [self hideLoading];
        if (succ) {
            [self showText:@"上传公司LOGO成功"];
            [self loadInfoData];
        }
        else {
            [self showText:@"图片上传失败，请重试"];
        }
    }];
}

- (void)editCompanyImages
{
    DDCompanyImageViewController *vc = [[DDCompanyImageViewController alloc] init];
    vc.companyInfo = self.companyInfo;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)editProductLogo {
    WEAKSELF;
    DDAvatarPickerView *avatar = [DDAvatarPickerView instanciate];
    avatar.showDefaultsImgs = NO;
    [avatar showWithAnimation:YES];
    avatar.didSelectAvatarType = ^(NSInteger type) {
        if (type == 0) {
            [[CCImageGetter sharedImageGetter] takePhoto];
            [CCImageGetter sharedImageGetter].callback = ^(UIImage *img) {
                [weakSelf uploadProductLogo:img];
            };
        }
        else if (type == 1) {
            [[CCImageGetter sharedImageGetter] selectFromAlbum];
            [CCImageGetter sharedImageGetter].callback = ^(UIImage *img) {
                [weakSelf uploadProductLogo:img];
            };
        }
    };
}

- (void)uploadProductLogo:(UIImage *)img {
    
    [self showLoading];
    [[DDCompanyHomeService sharedService] uploadProductLogo:img callback:^(BOOL succ) {
        [self hideLoading];
        if (succ) {
            [self showText:@"上传产品LOGO成功"];
            [self loadInfoData];
        }
        else {
            [self showText:@"图像上传失败，请重试"];
        }
    }];
}

- (void)editProductName {
    DDTextFieldEditViewController *vc = [[DDTextFieldEditViewController alloc] init];
    vc.title = @"产品名称";
    vc.value = self.companyInfo.product_name;
    vc.block = ^(NSString *value) {
        
        [self showLoading];
        DDProductSetInfo *info = [[DDProductSetInfo alloc] init];
        info.product_name = value;
        [[DDCompanyHomeService sharedService] setProductInfo:info cakkback:^(BOOL succ, NSString *err) {
            
            [self hideLoading];
            if (succ) {
                [self loadInfoData];
            }
            else {
                [self showErrorWithText:err];
            }
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)editProductAdvantage {
    DDTextViewEditViewController *vc = [[DDTextViewEditViewController alloc] initWithMaxCharacterNumber:500];
    vc.title = @"产品优势";
    vc.value = self.companyInfo.product_strengths;
    vc.block = ^(NSString *str) {
        
        [self showLoading];
        DDProductSetInfo *info = [[DDProductSetInfo alloc] init];
        info.product_strengths = str;
        [[DDCompanyHomeService sharedService] setProductInfo:info cakkback:^(BOOL succ, NSString *err) {
            
            [self hideLoading];
            if (succ) {
                [self loadInfoData];
            }
            else {
                [self showErrorWithText:err];
            }
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)editProductLight {
    DDTextViewEditViewController *vc = [[DDTextViewEditViewController alloc] initWithMaxCharacterNumber:500];
    vc.title = @"产品亮点";
    vc.value = self.companyInfo.product_highlights;
    vc.block = ^(NSString *str) {
        
        [self showLoading];
        DDProductSetInfo *info = [[DDProductSetInfo alloc] init];
        info.product_highlights = str;
        [[DDCompanyHomeService sharedService] setProductInfo:info cakkback:^(BOOL succ, NSString *err) {
            
            [self hideLoading];
            if (succ) {
                [self loadInfoData];
            }
            else {
                [self showErrorWithText:err];
            }
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

@end
